package com.zcf.controller;

import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.github.pagehelper.PageHelper;
import com.zcf.common.json.Body;
import com.zcf.common.result.PageResult;
import com.zcf.common.result.ResultVo;
import com.zcf.mapper.ChatMapper;
import com.zcf.pojo.Chat;
import com.zcf.pojo.User;
import com.zcf.utils.Hutool;
import com.zcf.vo.in.PageVo;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

/**
 * <p>
 * 聊天记录表 前端控制器
 * </p>
 *
 * @author loupeng
 * @since 2018-12-27
 */
@Controller
@RequestMapping("/chat")
@CrossOrigin
@ResponseBody
public class ChatController {

	@Autowired
	ChatMapper chatMapper;

	/**
	 * 查询聊天记录
	 */
	@PostMapping("queryChat")
	public Body queryChat(PageVo pv, String userId, String coverUserId) {
		EntityWrapper<Chat> ew = new EntityWrapper<>();
		ew.eq("user_id", userId);
		ew.eq("cover_user_id", coverUserId);
		ew.orderBy("addtime", false);
		if (pv.getPage() != null && pv.getPageSize() != null) {
			PageHelper.startPage(pv.getPage(), pv.getPageSize());
		}
		PageResult pr = PageResult.result(chatMapper.selectList(ew));
		ResultVo rv = new ResultVo();
		rv.setList(pr.getRows());
		rv.setTotal(pr.getTotal());

		if (rv == null) {
			return Body.BODY_500;
		}
		return Body.newInstance(rv);
	}

	@PostMapping("publics")
	public Body publics(Integer sum) {

		return Body.newInstance();
	}

	/**
	 * 增加聊天记录
	 */
	@PostMapping("insert")
	public Body insert(Chat chat) {
		chat.setChatId(Hutool.getId());
		chat.setAddtime(Hutool.parseDateToString());

		Boolean flag = chat.insert();
		if (flag) {
			return Body.newInstance(chat);
		}
		return Body.newInstance(-1, "新增失败");
	}

}
